package net.roobook.cms.modules.admin.controller;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import net.roobook.cms.common.annotation.Log;
import net.roobook.cms.common.annotation.Log.OperatorType;
import net.roobook.cms.common.entity.SearchEntity;
import net.roobook.cms.common.utils.CronUtils;
import net.roobook.cms.common.web.BaseController;
import net.roobook.cms.modules.admin.entity.Scheduled;
import net.roobook.cms.modules.admin.service.ScheduledService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

/**
 * @author cos
 * @email 327878610@qq.com
 * @link https://roobook.net
 * @Description 定时任务controller
 * @date 2023/9/25 21:08
 **/
@Controller
@RequestMapping("/admin/task")
public class ScheduledController extends BaseController {
    @Autowired
    private ApplicationContext context;

    @Autowired
    private ScheduledService scheduledService;


    /**
     * 查看任务列表
     */
    @Log(operType = OperatorType.PAGE, module = "计划任务", content = "计划任务分页列表")
    @RequestMapping({"","/list"})
    public String taskList(Model model, SearchEntity params) {
        PageInfo<Scheduled> page = scheduledService.queryListByPage(params);
        model.addAttribute("page", page);
        return "admin/task/list";
    }

    /**
     * 添加跳转
     */
    @Log(operType = OperatorType.OTHER, module = "计划任务", content = "添加计划任务页面")
    @RequestMapping("/toAdd")
    public String toAdd(Model model) {
        return "admin/task/add";
    }

    /**
     * 编辑任务cron表达式
     */
    @Log(operType = OperatorType.INSERT, module = "计划任务", content = "添加计划任务")
    @RequestMapping("/add")
    public String add(Scheduled scheduled) {
        if (!CronUtils.isValidExpression(scheduled.getCronExpression())) {
            throw new IllegalArgumentException("失败,非法表达式:" + scheduled.getCronExpression());
        }
        scheduled.setId(IdUtil.getSnowflakeNextIdStr());
        scheduled.setCreateBy(getUserId());
        scheduled.setCreateTime(new Date());
        int i = scheduledService.add(scheduled);
        return "redirect:/admin/task/list";
    }

    /**
     * 编辑跳转
     */
    @Log(operType = OperatorType.OTHER, module = "计划任务", content = "修改计划任务页面")
    @RequestMapping("/toEdit")
    public String toEdit(Model model,String id) {
        Scheduled scheduled = scheduledService.queryEntityById(id);
        model.addAttribute("scheduled", scheduled);
        return "admin/task/edit";
    }

    /**
     * 编辑任务
     */
    @Log(operType = OperatorType.UPDATE, module = "计划任务", content = "修改计划任务")
    @RequestMapping("/update")
    public String update(Scheduled scheduled) {
        if (!CronUtils.isValidExpression(scheduled.getCronExpression())) {
            throw new IllegalArgumentException("失败,非法表达式:" + scheduled.getCronExpression());
        }
        scheduled.setUpdateBy(getUserId());
        scheduled.setUpdateTime(new Date());
        int i = scheduledService.update(scheduled);
        return "redirect:/admin/task/list";
    }

    /**
     * 删除
     */
    @Log(operType = OperatorType.DELETE, module = "计划任务", content = "删除计划任务")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(Model model, String id) {
        scheduledService.delete(id);
        return "redirect:/admin/task/list";
    }

}
